package com.framework.spring.web;

import com.framework.spring.annotations.ResponseBody;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


@Getter
public class RequestHandler {

    private final Type type;

    private final Method method;

    private final Object bean;

    public RequestHandler(Method method, Object bean) {
        this.type = resolveType(method);
        this.method = method;
        this.bean = bean;
    }

    private Type resolveType(Method method) {
        if (method.isAnnotationPresent(ResponseBody.class)) {
            return Type.JSON;
        }
        return Type.HTML;
    }

    public Object handle(Object[] args) throws Exception {
        return method.invoke(bean, args);
    }

    enum Type {
        JSON, HTML;
    }
}
